home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zrelbit.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  7.3 KB  |  361 lines

  1. /* Copyright (C) 1989, 1995, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zrelbit.c,v 1.2 2000/09/19 19:00:55 lpd Exp $ */
  20. /* Relational, boolean, and bit operators */
  21. #include "ghost.h"
  22. #include "oper.h"
  23. #include "gsutil.h"
  24. #include "idict.h"
  25. #include "store.h"
  26.  
  27. /*
  28.  * Many of the procedures in this file are public only so they can be
  29.  * called from the FunctionType 4 interpreter (zfunc4.c).
  30.  */
  31.  
  32. /* ------ Standard operators ------ */
  33.  
  34. /* Define the type test for eq and its relatives. */
  35. #define EQ_CHECK_READ(opp, dflt)\
  36.     switch ( r_type(opp) ) {\
  37.     case t_string:\
  38.         check_read(*(opp));\
  39.         break;\
  40.     default:\
  41.         dflt;\
  42.   }
  43.  
  44. /* Forward references */
  45. private int obj_le(P2(os_ptr, os_ptr));
  46.  
  47. /* <obj1> <obj2> eq <bool> */
  48. int
  49. zeq(i_ctx_t *i_ctx_p)
  50. {
  51.     os_ptr op = osp;
  52.     EQ_CHECK_READ(op - 1, check_op(2));
  53.     EQ_CHECK_READ(op, DO_NOTHING);
  54.     make_bool(op - 1, (obj_eq(op - 1, op) ? 1 : 0));
  55.     pop(1);
  56.     return 0;
  57. }
  58.  
  59. /* <obj1> <obj2> ne <bool> */
  60. int
  61. zne(i_ctx_t *i_ctx_p)
  62. {    /* We'll just be lazy and use eq. */
  63.     int code = zeq(i_ctx_p);
  64.  
  65.     if (!code)
  66.     osp->value.boolval ^= 1;
  67.     return code;
  68. }
  69.  
  70. /* <num1> <num2> ge <bool> */
  71. /* <str1> <str2> ge <bool> */
  72. int
  73. zge(i_ctx_t *i_ctx_p)
  74. {
  75.     os_ptr op = osp;
  76.     int code = obj_le(op, op - 1);
  77.  
  78.     if (code < 0)
  79.     return code;
  80.     make_bool(op - 1, code);
  81.     pop(1);
  82.     return 0;
  83. }
  84.  
  85. /* <num1> <num2> gt <bool> */
  86. /* <str1> <str2> gt <bool> */
  87. int
  88. zgt(i_ctx_t *i_ctx_p)
  89. {
  90.     os_ptr op = osp;
  91.     int code = obj_le(op - 1, op);
  92.  
  93.     if (code < 0)
  94.     return code;
  95.     make_bool(op - 1, code ^ 1);
  96.     pop(1);
  97.     return 0;
  98. }
  99.  
  100. /* <num1> <num2> le <bool> */
  101. /* <str1> <str2> le <bool> */
  102. int
  103. zle(i_ctx_t *i_ctx_p)
  104. {
  105.     os_ptr op = osp;
  106.     int code = obj_le(op - 1, op);
  107.  
  108.     if (code < 0)
  109.     return code;
  110.     make_bool(op - 1, code);
  111.     pop(1);
  112.     return 0;
  113. }
  114.  
  115. /* <num1> <num2> lt <bool> */
  116. /* <str1> <str2> lt <bool> */
  117. int
  118. zlt(i_ctx_t *i_ctx_p)
  119. {
  120.     os_ptr op = osp;
  121.     int code = obj_le(op, op - 1);
  122.  
  123.     if (code < 0)
  124.     return code;
  125.     make_bool(op - 1, code ^ 1);
  126.     pop(1);
  127.     return 0;
  128. }
  129.  
  130. /* <num1> <num2> .max <num> */
  131. /* <str1> <str2> .max <str> */
  132. private int
  133. zmax(i_ctx_t *i_ctx_p)
  134. {
  135.     os_ptr op = osp;
  136.     int code = obj_le(op - 1, op);
  137.  
  138.     if (code < 0)
  139.     return code;
  140.     if (code) {
  141.     ref_assign(op - 1, op);
  142.     }
  143.     pop(1);
  144.     return 0;
  145. }
  146.  
  147. /* <num1> <num2> .min <num> */
  148. /* <str1> <str2> .min <str> */
  149. private int
  150. zmin(i_ctx_t *i_ctx_p)
  151. {
  152.     os_ptr op = osp;
  153.     int code = obj_le(op - 1, op);
  154.  
  155.     if (code < 0)
  156.     return code;
  157.     if (!code) {
  158.     ref_assign(op - 1, op);
  159.     }
  160.     pop(1);
  161.     return 0;
  162. }
  163.  
  164. /* <bool1> <bool2> and <bool> */
  165. /* <int1> <int2> and <int> */
  166. int
  167. zand(i_ctx_t *i_ctx_p)
  168. {
  169.     os_ptr op = osp;
  170.  
  171.     switch (r_type(op)) {
  172.     case t_boolean:
  173.         check_type(op[-1], t_boolean);
  174.         op[-1].value.boolval &= op->value.boolval;
  175.         break;
  176.     case t_integer:
  177.         check_type(op[-1], t_integer);
  178.         op[-1].value.intval &= op->value.intval;
  179.         break;
  180.     default:
  181.         return_op_typecheck(op);
  182.     }
  183.     pop(1);
  184.     return 0;
  185. }
  186.  
  187. /* <bool> not <bool> */
  188. /* <int> not <int> */
  189. int
  190. znot(i_ctx_t *i_ctx_p)
  191. {
  192.     os_ptr op = osp;
  193.  
  194.     switch (r_type(op)) {
  195.     case t_boolean:
  196.         op->value.boolval = !op->value.boolval;
  197.         break;
  198.     case t_integer:
  199.         op->value.intval = ~op->value.intval;
  200.         break;
  201.     default:
  202.         return_op_typecheck(op);
  203.     }
  204.     return 0;
  205. }
  206.  
  207. /* <bool1> <bool2> or <bool> */
  208. /* <int1> <int2> or <int> */
  209. int
  210. zor(i_ctx_t *i_ctx_p)
  211. {
  212.     os_ptr op = osp;
  213.  
  214.     switch (r_type(op)) {
  215.     case t_boolean:
  216.         check_type(op[-1], t_boolean);
  217.         op[-1].value.boolval |= op->value.boolval;
  218.         break;
  219.     case t_integer:
  220.         check_type(op[-1], t_integer);
  221.         op[-1].value.intval |= op->value.intval;
  222.         break;
  223.     default:
  224.         return_op_typecheck(op);
  225.     }
  226.     pop(1);
  227.     return 0;
  228. }
  229.  
  230. /* <bool1> <bool2> xor <bool> */
  231. /* <int1> <int2> xor <int> */
  232. int
  233. zxor(i_ctx_t *i_ctx_p)
  234. {
  235.     os_ptr op = osp;
  236.  
  237.     switch (r_type(op)) {
  238.     case t_boolean:
  239.         check_type(op[-1], t_boolean);
  240.         op[-1].value.boolval ^= op->value.boolval;
  241.         break;
  242.     case t_integer:
  243.         check_type(op[-1], t_integer);
  244.         op[-1].value.intval ^= op->value.intval;
  245.         break;
  246.     default:
  247.         return_op_typecheck(op);
  248.     }
  249.     pop(1);
  250.     return 0;
  251. }
  252.  
  253. /* <int> <shift> bitshift <int> */
  254. int
  255. zbitshift(i_ctx_t *i_ctx_p)
  256. {
  257.     os_ptr op = osp;
  258.     int shift;
  259.  
  260.     check_type(*op, t_integer);
  261.     check_type(op[-1], t_integer);
  262. #define MAX_SHIFT (arch_sizeof_long * 8 - 1)
  263.     if (op->value.intval < -MAX_SHIFT || op->value.intval > MAX_SHIFT)
  264.     op[-1].value.intval = 0;
  265. #undef MAX_SHIFT
  266.     else if ((shift = op->value.intval) < 0)
  267.     op[-1].value.intval = ((ulong)(op[-1].value.intval)) >> -shift;
  268.     else
  269.     op[-1].value.intval <<= shift;
  270.     pop(1);
  271.     return 0;
  272. }
  273.  
  274. /* ------ Extensions ------ */
  275.  
  276. /* <obj1> <obj2> .identeq <bool> */
  277. private int
  278. zidenteq(i_ctx_t *i_ctx_p)
  279. {
  280.     os_ptr op = osp;
  281.  
  282.     EQ_CHECK_READ(op - 1, check_op(2));
  283.     EQ_CHECK_READ(op, DO_NOTHING);
  284.     make_bool(op - 1, (obj_ident_eq(op - 1, op) ? 1 : 0));
  285.     pop(1);
  286.     return 0;
  287.  
  288. }
  289.  
  290. /* <obj1> <obj2> .identne <bool> */
  291. private int
  292. zidentne(i_ctx_t *i_ctx_p)
  293. {
  294.     /* We'll just be lazy and use .identeq. */
  295.     int code = zidenteq(i_ctx_p);
  296.  
  297.     if (!code)
  298.     osp->value.boolval ^= 1;
  299.     return code;
  300. }
  301.  
  302. /* ------ Initialization procedure ------ */
  303.  
  304. const op_def zrelbit_op_defs[] =
  305. {
  306.     {"2and", zand},
  307.     {"2bitshift", zbitshift},
  308.     {"2eq", zeq},
  309.     {"2ge", zge},
  310.     {"2gt", zgt},
  311.     {"2le", zle},
  312.     {"2lt", zlt},
  313.     {"2.max", zmax},
  314.     {"2.min", zmin},
  315.     {"2ne", zne},
  316.     {"1not", znot},
  317.     {"2or", zor},
  318.     {"2xor", zxor},
  319.         /* Extensions */
  320.     {"2.identeq", zidenteq},
  321.     {"2.identne", zidentne},
  322.     op_def_end(0)
  323. };
  324.  
  325. /* ------ Internal routines ------ */
  326.  
  327. /* Compare two operands (both numeric, or both strings). */
  328. /* Return 1 if op[-1] <= op[0], 0 if op[-1] > op[0], */
  329. /* or a (negative) error code. */
  330. private int
  331. obj_le(register os_ptr op1, register os_ptr op)
  332. {
  333.     switch (r_type(op1)) {
  334.     case t_integer:
  335.         switch (r_type(op)) {
  336.         case t_integer:
  337.             return (op1->value.intval <= op->value.intval);
  338.         case t_real:
  339.             return ((double)op1->value.intval <= op->value.realval);
  340.         default:
  341.             return_op_typecheck(op);
  342.         }
  343.     case t_real:
  344.         switch (r_type(op)) {
  345.         case t_real:
  346.             return (op1->value.realval <= op->value.realval);
  347.         case t_integer:
  348.             return (op1->value.realval <= (double)op->value.intval);
  349.         default:
  350.             return_op_typecheck(op);
  351.         }
  352.     case t_string:
  353.         check_read(*op1);
  354.         check_read_type(*op, t_string);
  355.         return (bytes_compare(op1->value.bytes, r_size(op1),
  356.                   op->value.bytes, r_size(op)) <= 0);
  357.     default:
  358.         return_op_typecheck(op1);
  359.     }
  360. }
  361.